package stu.aistar.util;


import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传的工具类
 */
@Component
public class FileUtils {
    //定义一个目标路径，把文件上传到指定位置
    public static final String BASE_PATH="D:\\nginx-1.19.10\\html\\movies\\";

    //定义访问图片的路径
    public static final String SERVER_PATH = "http://localhost:80/movies/";

    public static String upload(MultipartFile file){
        //获得上传文件的路径
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-","");
        String newFilename = uuid+'-'+filename;
        //创建一个文件实例对象
        File image = new File(BASE_PATH,newFilename);
        //对该文件进行上传操作
        try {
            file.transferTo(image);
        }catch (IOException e){
            return null;
        }
        return newFilename;
    }
}

